﻿using DGIS.Service.IService.IRenderService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;

namespace DGIS.Service.Service.RenderService
{
    public class RasterLayerRenderService : IRasterLayerRenderService
    {
        public IRasterLayer CreateClassBreaksRender(IRasterLayer layer,Dictionary<double, ISymbol> symbols, List<string> labels)
        {
            IRasterClassifyColorRampRenderer rasterClassifyColorRampRenderer = new RasterClassifyColorRampRendererClass();
            IRasterRenderer rasterRender = (IRasterRenderer)rasterClassifyColorRampRenderer;
            IRaster raster = layer.Raster;

            IRasterBandCollection pRBandCol = raster as IRasterBandCollection;
            IRasterBand band = pRBandCol.Item(0);

            //如果是连续影像，没有数值直方图，则构建数值直方图
            if (band.Histogram == null)
                band.ComputeStatsAndHist();

            //设置分级方式
            IClassifyGEN classifyGEN = new EqualIntervalClass();
            IRasterClassifyUIProperties rasterClassifyUIProperties = rasterClassifyColorRampRenderer as IRasterClassifyUIProperties;
            rasterClassifyUIProperties.ClassificationMethod = classifyGEN.ClassID;
            rasterRender.Raster = raster;
            //设置分类级数
            rasterClassifyColorRampRenderer.ClassCount = symbols.Count;
            rasterRender.Update();

            for (int i = 0; i < symbols.Count; i++)
            {
                rasterClassifyColorRampRenderer.set_Symbol(i, symbols.ElementAt(i).Value);
                rasterClassifyColorRampRenderer.set_Break(i, symbols.ElementAt(i).Key);

                //构造显示的文字 
                rasterClassifyColorRampRenderer.set_Label(i, labels[i]);
                rasterClassifyColorRampRenderer.set_Description(i, labels[i]);
            }

            rasterRender.Update();
            layer.Renderer = rasterRender;

            return layer;
        }

        public IRasterLayer CreateUniqueRender(IRasterLayer layer, Dictionary<double, ISymbol> symbols, List<string> labels)
        {
            IRasterUniqueValueRenderer rasterUniqueValueRender = new RasterUniqueValueRendererClass();
            IRasterRenderer rasterRender = (IRasterRenderer)rasterUniqueValueRender;
            IRaster raster = layer.Raster;

            IRasterBandCollection pRBandCol = raster as IRasterBandCollection;
            IRasterBand band = pRBandCol.Item(0);

            //如果是连续影像，没有数值直方图，则构建数值直方图
            if (band.Histogram == null)
                band.ComputeStatsAndHist();

            rasterUniqueValueRender.HeadingCount = 1;
            rasterUniqueValueRender.set_ClassCount(0, symbols.Count);

            rasterRender.Raster = raster;
            rasterRender.Update();

            for (int i = 0; i < symbols.Count; i++)
            {
                rasterUniqueValueRender.AddValue(0,i, symbols.ElementAt(i).Key);
                rasterUniqueValueRender.set_Symbol(0, i, symbols.ElementAt(i).Value);
                rasterUniqueValueRender.set_Label(0,i, labels[i]);
            }

            rasterRender.Update();
            layer.Renderer = rasterRender;

            return layer;
        }        
    }
}
